package fun.eliauk.treedemo.utils.encrypt;

import java.util.ArrayList;
import java.util.List;

public class TextUtil {
    public static List<String> text2Slice(String input, int groupSize) {
        if (input == null || input.isEmpty() || groupSize <= 0) {
            throw new IllegalArgumentException("输入字符串不能为空且分组大小必须大于0");
        }

        List<String> result = new ArrayList<>();
        int length = input.length();

        if (groupSize > length) {
            // 处理分组大小大于字符串长度的情况
            throw new IllegalArgumentException("分组大小不能大于输入字符串长度");
        }

        for (int i = 0; i <= length - groupSize; i++) {
            StringBuilder group = new StringBuilder();
            for (int j = 0; j < groupSize; j++) {
                group.append(String.valueOf(input.charAt(i + j)));
            }
            result.add(group.toString());
        }
        return result;
    }


    public static List<String> text2Group(String input,int groupSize){
        if (input == null || input.isEmpty() || groupSize <= 0) {
            throw new IllegalArgumentException("输入字符串不能为空且分组大小必须大于0");
        }
        int length = input.length();
        if (length % groupSize  != 0){
            throw new IllegalArgumentException("不符合分組条件");
        }
       List<String> result = new ArrayList<>();
        for (int i = 0; i <length-groupSize; i+=groupSize) {
            String s = input.substring(i,i+groupSize);
            result.add(s);
        }
        return result;
    }

    public static void main(String[] args) {
        String input = "13481322177";
        int groupSize = 4;

        List<String> result = text2Slice(input, groupSize);

        // 打印结果
        for (String group : result) {
            System.out.println(group);
        }

    }
}
